/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Thanh
 */
public class GetUserDAO {

    public User getUser(String id) {
        User result = new User();
        try {
            String un = "sa";
            String pw = "1234";
            String url = "jdbc:sqlserver://localhost:1433;databaseName=BloggerDB;";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = java.sql.DriverManager.getConnection(url, un, pw);

            String sql = "select u.UserID, r.RoleName, u.Username, u.FullName, u.Birthday, u.Gender, u.Address, u.Email, u.VisitCount, u.CreatedTime, u.ProfilePicture from Users u left join Roles r on u.RoleID = r.RoleID where u.UserID = ?";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, id);
            ResultSet rs = stm.executeQuery();

            while (rs.next()) {
                result.setUserID(rs.getInt("UserID"));
                result.setRole(rs.getString("RoleName"));
                result.setUsername(rs.getString("Username"));

                result.setFullName(rs.getString("FullName"));
                result.setBirthday(rs.getDate("Birthday"));
                if(rs.getBoolean("Gender"))
                {
                    result.setGender("Male");
                } else if(!rs.getBoolean("Gender"))
                {
                    result.setGender("Female");
                }
                result.setProfilePicture(rs.getString("ProfilePicture"));
                result.setAddress(rs.getString("Address"));
                result.setEmail(rs.getString("Email"));
                result.setVisitCount(rs.getInt("VisitCount"));
                result.setCreatedTime(rs.getDate("CreatedTime").toString() + " " + rs.getTime("CreatedTime").toString());
            }
            rs.close();
            stm.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
